package com.itown.expo.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import webframe.common.file.ExportDataHelper;
import webframe.common.file.RowMapperFactory;
import webframe.common.helper.Code2NameHelper;
import webframe.common.helper.Code2NameImportEntry;
import webframe.common.helper.FormatTransformHelper;

import com.itown.expo.model.Decl;
import com.itown.expo.model.Goods;

public class StatGoodsListExcelView extends AbstractExcelView {
	private static Log logger = LogFactory.getLog(StatGoodsListExcelView.class);
	private static Map<String, Code2NameImportEntry> c2NStorge;

	private RowMapperFactory rowMapperFactory;

	public RowMapperFactory getRowMapperFactory() {
		return rowMapperFactory;
	}

	public void setRowMapperFactory(RowMapperFactory rowMapperFactory) {
		this.rowMapperFactory = rowMapperFactory;
	}

	@Override
	protected void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		WebApplicationContext wc = getWebApplicationContext();
		if (c2NStorge == null) {
			c2NStorge = Code2NameHelper.getCode2StringStorage(wc);
			// style
		}
		response.setContentType("APPLICATION/OCTET-STREAM");
		String filename = "货物列表-" + FormatTransformHelper.currentDate()
				+ ".xls";
		filename = new String(filename.getBytes("GB18030"), "ISO8859-1");
		response.setHeader("Content-Disposition", "attachment; filename=\""
				+ filename + "\"");
		logger.info("导出>货物列表-报检单列表");

		ExportDataHelper.exportDataToExcel(c2NStorge, workbook, "货物列表",
				(List) model.get("goodsList"), getRowMapperFactory()
						.getRowMapper(wc, Goods.class));
		StatDeclListExcelView.customWorkSheet1(workbook, (List<Decl>) model
				.get("declList"));// 上海局世博专用物资进境统计表

	}

}
